﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Toon.Models.Characters;
using Toon.Models.Catalogs;

namespace Toon
{
	public partial class MainPage : UserControl
	{
		public MainPage()
		{
			InitializeComponent();

			DataContext = new PrimaryViewModel(ContentFrame) { CurrentRuleSet = new StandardRules(new StandardCatalog())};
		}

		// After the Frame navigates, ensure the HyperlinkButton representing the current page is selected
		private void ContentFrame_Navigated(object sender, NavigationEventArgs e)
		{
			foreach (var child in LinksStackPanel.Children.OfType<HyperlinkButton>())
			{
				if (child.NavigateUri != null)
				{
					if (child.NavigateUri.ToString().Equals(e.Uri.ToString()))
						VisualStateManager.GoToState(child, "ActiveLink", true);
					else
						VisualStateManager.GoToState(child, "InactiveLink", true);
				}
			}
		}

		// If an error occurs during navigation, show an error window
		private void ContentFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
		{
			e.Handled = true;
			ChildWindow errorWin = new ErrorWindow(e.Uri);
			errorWin.Show();
		}
	}
}